package com.apache.bid.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.bid.controller.BaseSupperAction;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidMaxContent;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidMaxContentManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.bid.manager.UnityBaseManager;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.uct.common.PBOSSOTools;

@Controller
@RequestMapping("/admin/content")
public class BidMaxContentAction extends BaseSupperAction<BidMaxContent> {

	@Autowired
	private BidMaxContentManager bidMaxContentManager;

	@Autowired
	private BidSessionManager bidSessionManager;

	@Autowired
	private BidItemManager bidItemManager;

	/**
	 * 后台获取列表数据
	 * @param request
	 * @param objType
	 * @param objId
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView toListPage(HttpServletRequest request, String objType, String objId, String direction) {
		ModelAndView view = new ModelAndView("admin/viewJsp/content");
		view.addObject("objType", objType);
		view.addObject("objId", objId);

		ParamsVo vo = new ParamsVo();
		vo.setInfoId(objId);
		String objName = "";
		if (objType.equals("session")) {
			BidSession s = (BidSession) bidSessionManager.getInfoById(vo);
			objName = s.getSessionName();
		} else if (objType.equals("item")) {
			BidItem i = (BidItem) bidItemManager.getInfoById(vo);
			objName = i.getItemName();
		}
		if ("F".equals(direction)) {
			view.addObject("ggname", "采购公告");
		} else {
			view.addObject("ggname", "交易公告");
		}
		view.addObject("objName", objName);
		return view;
	}

	/**
	 *前台查询列表分页数据，返回JSON数据
	 */
	@RequestMapping(value = { "/rf" }, method = { RequestMethod.GET })
	@ResponseBody
	public Object listData(HttpServletRequest request, BidMaxContent item, String page, String rows) {
		ParamsVo<BidMaxContent> vo = new ParamsVo<BidMaxContent>();
		//		if(Validator.isNotNull(item.getContentType())){
		//			try {
		//				String contentTypeName = new String(item.getContentType().getBytes("ISO8859-1"),"utf-8");
		//				item.setContentType(contentTypeName);
		//			} catch (Exception e) {
		//				e.printStackTrace();
		//			}
		//		}
		vo.setObj(item);
		Map<String, Object> mapData = new HashMap<String, Object>();
		if (Validator.isNotNull(rows)) {//rows为空表示不进行分页处理
			vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
			vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
			Page pageList = getBaseManager().getPageInfo(vo);//分页
			mapData.put("total", pageList.getCount());//dataGrid所需参数
			mapData.put("rows", pageList.getPageObjects());//dataGrid所需数据对象集合
		} else {
			List<BidMaxContent> list = getBaseManager().getList(vo);
			if (!Validator.isEmpty(list)) {
				mapData.put("total", list.size());//dataGrid所需参数
				mapData.put("rows", list);//dataGrid所需数据对象集合
			}
		}
		return mapData;
	}

	@Override
	public ResultMsg saveInfo(HttpServletRequest request, HttpServletResponse response, BidMaxContent bidCont) {
		ResultMsg rmsg = new ResultMsg("F", "操作失败！");
		if (Validator.isEmpty(bidCont) || Validator.isNull(bidCont.getContentType())
				|| Validator.isNull(bidCont.getTitle()) || Validator.isNull(bidCont.getObjId())
				|| Validator.isNull(bidCont.getObjType()))
			return new ResultMsg("F", "缺少必要的参数！");
		ParamsVo<BidMaxContent> vo = new ParamsVo<BidMaxContent>();
		vo.setKey("checkIsUnique");
		vo.setObj(bidCont);
		Object result = bidMaxContentManager.execute(vo);
		if ("false".equals(String.valueOf(result))) {
			rmsg.setFlag("F");
			rmsg.setMsg("不能重复添加！");
			return rmsg;
		}

		ParamsVo<BidMaxContent> voSave = this.getParamsVo(request);//初始化参数vo对象，如果是登陆用户则获取用户信息及日志
		voSave.setParams("loginUser", PBOSSOTools.getLoginUser(request));//存放当前登录用户
		voSave.setObj(bidCont);//新增
		String id = getBaseManager().saveInfo(vo);
		if (Validator.isNull(id)) {
			rmsg = new ResultMsg("F", "新增条目失败，请重试!");
		}
		rmsg.setFlag("T");
		rmsg.setMsg("操作成功！");
		return rmsg;
	}

	@Override
	public ResultMsg editInfo(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute BidMaxContent item, @PathVariable String id) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");//用于封装结果信息
		if (Validator.isNull(id)) {
			rmsg = new ResultMsg("F", "sessionId不能为空！");
			return rmsg;
		}

		ParamsVo<BidMaxContent> vo = new ParamsVo<BidMaxContent>();
		vo.setKey("checkIsUnique");
		vo.setObj(item);
		Object result = bidMaxContentManager.execute(vo);
		if ("false".equals(String.valueOf(result))) {
			rmsg.setFlag("F");
			rmsg.setMsg("标题不能重复！");
			return rmsg;
		}

		ParamsVo<BidMaxContent> vo1 = this.getParamsVo(request);//初始化参数vo对象，如果是登陆用户则获取用户信息及日志
		vo1.setObj(item);//修改
		boolean mark = getBaseManager().editInfo(vo1);
		if (!mark) {
			rmsg = new ResultMsg("F", "操作失败！");
		}
		return rmsg;
	}

	@Override
	protected UnityBaseManager<BidMaxContent> getBaseManager() {
		return bidMaxContentManager;
	}

	@Override
	protected String getCommBoxValue(BidMaxContent item, String type) {
		return null;
	}

}
